-- stop_action
-- create by panyl
-- 技能: 时间停止

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 持续回合
        local effectRound = para[1];

        -- BOSS层持续回合为3
        local dungeonId = DungeonM.getDungeonId();
        local layer = DungeonM.currentLayer();
        if DungeonBossM.isBossLayer(dungeonId, layer) or (UnrealDungeonM.isInUnreal() and UnrealDungeonM.getUnrealId() == 2) then
            effectRound = 3;
        end

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        -- 最终威力
        effectRound = math.modf(effectRound * (addon_rate + 1000)/1000);

        -- 属性：持续时间+
        local addonRound = 0;
        local prop = PropM.combine(source, "magic_last", skillId);
        addonRound = PropM.apply(prop, addonRound);
        prop = PropM.combine(source, "magic_last", "assist");
        addonRound = PropM.apply(prop, addonRound);
        prop = PropM.combine(source, "magic_last", "all");
        addonRound = PropM.apply(prop, addonRound);

        -- 如果是boss层或者特殊夹层迷宫，减半
        if DungeonBossM.isBossLayer(dungeonId, layer) or
          (UnrealDungeonM.isInUnreal() and (UnrealDungeonM.getUnrealId() == 2) or ArenaM.isInArena())
           then
            addonRound  = extraCeil(addonRound, 2);
        end
        effectRound = effectRound + addonRound;

        if para[2] > 0 then
            effectRound = para[1];
        end

        extra = extra or {};
        local lastRound = extra.lastRound;

        -- 有指定的回合数
        if type(lastRound) == "number" then
            effectRound = lastRound;
        end

        local condition = {
            ["end_round"] = round + effectRound + 1,
        };

        -- 附加施法者的状态
        CombatStatusM.applyStatus(target, "stop_action", condition);

        -- 增加命中动作（不需要命中动作）
        -- SkillM.getSequence(target):hit(source, target, skillId);

        -- 所有格子
        local grids = DungeonM.getCurrentDungeon();
        local ret = {};
        for _, grid in pairs(grids) do
            -- 需要过滤如下怪物：已死亡的怪、免疫的怪物、观众
            if grid:isMonster() and
                not grid.monster:isDead() and
                NormalAudienceM.allowStopAction(grid.monster) then

                if not SkillM.immunitySkill(source, grid.monster, skillId) then
                    -- 增加命中动作
                    --SkillM.getSequence(target):hit(source, target, skillId);

                    -- 附加状态
                    CombatStatusM.applyStatus(grid.monster, "stop_action", condition);

                    EventMgr.fire(event.GRID_LOCK_STATE, {["pos"] = grid:getPos(), });

                    -- 目标
                    table.insert(ret, grid.monster);
                else
                    -- 先初始化
                    SkillM.initSequence(source, grid.monster, skillId);

                    SkillM.getSequence(grid.monster):immunity(source, grid.monster, skillId);
                end
            end
        end

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), }
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 回合
        local baseRound = para[1];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        -- 最终威力
        local effectRound = math.modf(baseRound * (addon_rate + 1000)/1000);

        -- 1. 持续回合加成
        local addRound = 0;
        local prop = PropM.combine(source, "magic_last", skillId);
        addRound = PropM.apply(prop, addRound);
        prop = PropM.combine(source, "magic_last", "assist");
        addRound = PropM.apply(prop, addRound);
        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        addRound = PropM.apply(prop, addRound);
        effectRound = effectRound + addRound;

        if para[2] > 0 then
            effectRound = para[1];
        end

        -- 普通怪效果描述
        local addonRound = effectRound - baseRound;
        if addonRound == 0 or para[2] > 0 then
            desc = string.gsub(desc, "{round}", effectRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        -- BOSS及PVP?的计算
        baseRound = 3;
        effectRound = math.modf(baseRound * (addon_rate + 1000)/1000);

        -- boss持续时间+属性，需要减半
        addRound = extraCeil(addRound, 2);
        effectRound = effectRound + addRound;

        -- 持续时间
        addonRound = effectRound - baseRound;
        if addonRound == 0 or para[2] > 0 then
            desc = string.gsub(desc, "{roundBoss}", effectRound);
        else
            desc = string.gsub(desc, "{roundBoss}", baseRound .. "(+" .. addonRound .. ")");
        end

        return desc;
    end,
}
